package com.haochu.xxl.animate;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.haochu.xxl.model.PointItem;

/**
 * @author Administrator 元素交换位置动画
 */
public class ChangeAnimation extends Animation {
	// 游戏界面view
	private View view;
	// 元素点
	private PointItem p1, p2;
	// 两点的初始坐标
	private int p1x, p1y, p2x, p2y;
	// 两点的坐标差
	private int diffX, diffY;

	public ChangeAnimation(View view, PointItem p1, PointItem p2) {
		this.p1 = p1;
		this.p2 = p2;
		this.view = view;
		this.diffX = p1.getX() - p2.getX();
		this.diffY = p1.getY() - p2.getY();
		this.p1x = p1.getX();
		this.p1y = p1.getY();
		this.p2x = p2.getX();
		this.p2y = p2.getY();
		this.setDuration(200);
	}

	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		super.applyTransformation(interpolatedTime, t);
		p1.setX(p1x - (int) (diffX * interpolatedTime));
		p1.setY(p1y - (int) (diffY * interpolatedTime));
		p2.setX(p2x + (int) (diffX * interpolatedTime));
		p2.setY(p2y + (int) (diffY * interpolatedTime));
		view.postInvalidate();
	}
}